package com.zhaofujun.filequeue;

import java.nio.ByteBuffer;

public class HeadInfo {
    private final long position;
    /**
     * 偏移量
     */
    private final long bodyOffset;
    /**
     * 消息大小
     */
    private final int bodySize;

    public long getBodyOffset() {
        return bodyOffset;
    }

    public int getBodySize() {
        return bodySize;
    }

    public long getPosition() {
        return position;
    }

    public HeadInfo(long position, long bodyOffset, int bodySize) {
        this.position = position;
        this.bodyOffset = bodyOffset;
        this.bodySize = bodySize;
    }

    public static final int SIZE = Long.BYTES + Long.BYTES + Integer.BYTES;

    public byte[] toBytes() {
        byte[] bytes = new byte[SIZE];
        ByteBuffer byteBuffer = ByteBuffer.wrap(bytes);
        byteBuffer.putLong(position);
        byteBuffer.putLong(bodyOffset);
        byteBuffer.putInt(bodySize);
        return bytes;
    }

    public static HeadInfo fromBytes(byte[] bytes) {
        ByteBuffer byteBuffer = ByteBuffer.wrap(bytes);
        long position = byteBuffer.getLong();
        long offset = byteBuffer.getLong();
        int size = byteBuffer.getInt();
        return new HeadInfo(position, offset, size);
    }
        @Override
        public String toString() {
            return "HeadInfo{" +
                    "position=" + position +
                    ", bodyOffset=" + bodyOffset +
                    ", bodySize=" + bodySize +
                    '}';
        }
}
